import { app, BrowserWindow, ipcMain, Menu } from "electron";
const isDev = require("electron-is-dev");
import * as path from "path";
import { filsInit } from "./util/file";
import { handleUpdate, dowlandUpdate } from "./util/autoUpdater";
import initHandle from "./util/bindHandle";
import config, { env } from "./config";
import { menutemplate } from "./util/menuTemplate";

filsInit();
let mainWindow = null;
function createWindow() {
  process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true";
  // process.env["NODE_ENV"] = "development";
  // Create the browser window.
  mainWindow = new BrowserWindow({
    maxWidth: 1000,
    maxHeight: 900,
    height: 700,
    webPreferences: {
      nodeIntegration: true, // 可以在渲染层调用node的require
      // 在electron 10.0.0之后，remote模块默认关闭
      // 必须手动设置webPreferences中的enableRemoteModule为true之后才能使用
      enableRemoteModule: true, // 这里是关键设置
      contextIsolation: false,
      preload: path.join(__dirname, "preload.js"),
    },
    width: 900,
  });
  mainWindow.loadURL("http://192.168.60.31:3000/"); // 这里改为vue的启动目录
  if (isDev) {
    //    // mainWindow.loadFile("./vue_web/dist/index.html");
    // // Open the DevTools.
    mainWindow.webContents.openDevTools();
  } else {
    // mainWindow.loadFile(path.join(__dirname, "../build_dist/index.html"));
  }

  dowlandUpdate(mainWindow);
  initHandle(mainWindow);

  // // and load the index.html of the app.

  // // Open the DevTools.
  // mainWindow.webContents.openDevTools();
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on("ready", () => {
  createWindow();
  //设置应用菜单
  const me = Menu.buildFromTemplate(menutemplate);
  Menu.setApplicationMenu(me);

  app.on("activate", function () {
    // On macOS it's common to re-create a window in the app when the
    // dock icon is clicked and there are no other windows open.
    if (BrowserWindow.getAllWindows().length === 0) createWindow();
  });
});

// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

// In this file you can include the rest of your app"s specific main process
// code. You can also put them in separate files and require them here.
